package com.guo.worktool.Controller;

import com.alibaba.fastjson2.JSON;
import com.guo.worktool.common.comps.RedisUtil;
import com.guo.worktool.common.core.AjaxResult;
import com.guo.worktool.common.enums.EnumSQLType;
import com.guo.worktool.common.model.RequestWorkToolBody;
import com.guo.worktool.manager.AsyncManager;
import com.guo.worktool.manager.factory.AsyncFactory;
import com.guo.worktool.utils.ServletUtils;
import com.guo.worktool.utils.StringUtils;
import com.guo.worktool.utils.ip.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.guo.worktool.common.core.AjaxResult.success;
import static com.guo.worktool.service.impl.SQLWorkToolServiceImpl.*;

/**
 * @program: WorkTool
 * @author: Mr.GUO
 * @create: 2023-03-13 12:27
 **/
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/AssemblyData")
public class SQLWorkToolController {
    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("sendMsg")
    public AjaxResult sendMsg(@RequestBody RequestWorkToolBody requestWorkToolBody) {
        //获取客户访问ip
        final String ipaddr = IpUtils.getIpAddr(ServletUtils.getRequest());
        AsyncManager.me().execute(() -> AsyncFactory.getVisitorRecords(redisUtil, ipaddr));
        String s = "";
        log.info("sendMsg:{}", JSON.toJSONString(requestWorkToolBody));
        String[] split = requestWorkToolBody.getStr().trim().split("\n");
        String type = requestWorkToolBody.getType();
        if (StringUtils.isEmpty(type) || split.length < 1) {
            s = "造轮子是个技术活，你超出了轮子的范围！";
        } else if (type.equals(EnumSQLType.用户id.getValue())) {
            s = assemblyDataByIdorName(split, "id");
        } else if (type.equals(EnumSQLType.用户name.getValue())) {
            s = assemblyDataByIdorName(split, "name");
        } else if (type.equals(EnumSQLType.身份证号码idNo.getValue())) {
            s = assemblyDataByIdNo(split);
        } else if (type.equals(EnumSQLType.MAP.getValue()) && requestWorkToolBody.getStr().trim().contains(":")) {
            s = assemblyDataOutForm(split);
        } else if (type.equals(EnumSQLType.原生表单.getValue()) && requestWorkToolBody.getStr().trim().contains(":")) {
            s = assemblyDataOutForm0(split);
        } else if (type.equals(EnumSQLType.JSON.getValue()) && requestWorkToolBody.getStr().trim().contains(":")) {
            s = assemblyDataOutFormJSON(requestWorkToolBody.getStr().trim());
        } else {
            s = "造轮子是个技术活，你超出了轮子的范围！";
        }
        log.info("response");
        return success(s);
    }
}



